package ko;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import android.util.Log;

public class KGame {
	private ArrayList<KEntity> _mainList;
	private Hashtable<String, List<KEntity>> _renderList;
	private ArrayList<String> _layerList;
	private int renderIndex,layerIndex;
	
	public KGame() {
		_layerList = new ArrayList<String>();
		_renderList = new Hashtable<String, List<KEntity>>();
		_layerList.add("first");
		_layerList.add("last");
		this._renderList.put(_layerList.get(0), new ArrayList<KEntity>());
		this._renderList.put(_layerList.get(1), new ArrayList<KEntity>());
		this._mainList = new ArrayList<KEntity>();
	}


	public KEntity getEntity(String name, String layer) {
		KEntity entity;
		ArrayList<KEntity> list = (ArrayList<KEntity>) ((layer == "main") ? this._mainList
				: this._renderList.get(layer));

		int index = list.size();
		while (index-- > 0){
			entity = list.get(index);
			if (entity.name == name){
				return entity;
			}
		}
		return null;
	}
	
	public String CheckLayerName(String layer) {
		if (this._renderList.get(layer) == null) {
			Log.i("error","not layer !!!");
			return "";
		}
		return layer;
	}
	
	public ArrayList<KEntity> getRenderByLayer(String layer) {
		return (ArrayList<KEntity>) this._renderList.get(layer);
	}

	public void addLayer(String layer) {
		this._layerList.add(this._layerList.size() - 1, layer);
		this._renderList.put(layer, new ArrayList<KEntity>());
	}
	
	public void add(KEntity entity) {
		addHelper(entity,null);
	}	
	public void add(KEntity entity, String layer) {
		addHelper(entity,layer);
	}	
	public void addHelper(KEntity entity, String layer) {
		layer = layer == null ? "last" : layer;
		if (entity._world == this) {
			return;
		}
		if (layer == "main") {
			_mainList.add(entity);
			entity._world = this;
			entity._layer = "main";
			return;
		} else if (this._renderList.get(layer) == null) {
			addLayer(layer);
		}
		((ArrayList<KEntity>) this._renderList.get(layer)).add(entity);
		entity._world = this;
		entity._layer = layer;
	}

	public void remove(KEntity entity) {
		int index = -1;
		if (entity._layer == "main") {
			index = _mainList.indexOf(entity);
			if (index > -1) {
				removeEntity(entity);
				_mainList.remove(index);
			}
		} else {
			ArrayList<KEntity> renderList = (ArrayList<KEntity>) this._renderList.get(entity._layer);
			index = renderList.indexOf(entity);
			if (index > -1) {
				removeEntity(entity);
				renderList.remove(index);
			}
		}
	}

	public void removeAll() {
		KEntity entity = null;
		ArrayList<KEntity> renderList;
		String layer;		
		layerIndex = this._layerList.size();
		while (layerIndex-- > 0) {
			layer = this._layerList.get(layerIndex);
			renderList = (ArrayList<KEntity>) this._renderList.get(layer);
			renderIndex = renderList.size();
			while (renderIndex-- > 0) {
				entity = renderList.get(renderIndex);
				removeEntity(entity);
				renderList.remove(renderIndex);
			}
			renderList = new ArrayList<KEntity>();
		}
		int mainIndex = _mainList.size();
		while (mainIndex-- > 0) {
			entity = _mainList.get(mainIndex);
			removeEntity(entity);
			_mainList.remove(mainIndex);
		}
		_mainList = new ArrayList<KEntity>();
		//renderIndex = layerIndex = 0;
	}

	private void removeEntity(KEntity entity) {
		entity.removed();
		entity._world = null;
	}

	public void update() {
		ArrayList<KEntity> renderList;		
		String layer;
		layerIndex = this._layerList.size();
		while (layerIndex-- > 0) {
			layer = this._layerList.get(layerIndex);
			renderList = getRenderByLayer(layer);
			renderIndex = renderList.size();
			while (renderIndex-- > 0) {				
				UpdateHelper(renderList.get(renderIndex));
			}
		}
	}

	public void render() {
		if(K.stage == null) return;
		K.stage.lockCanvas();
		
		ArrayList<KEntity> renderList;		
		String layer;		
		int renderCount, layerCount = _layerList.size();
		layerIndex = 0;
		while (layerIndex < layerCount) {
			layer = this._layerList.get(layerIndex++);
			renderList = getRenderByLayer(layer);
			renderIndex = 0;
			renderCount = renderList.size();
			while (renderIndex < renderCount) {
				RenderHelper(renderList.get(renderIndex++));
			}
		}
		mainRender();
		
		
		K.view.drawBitmap(K.bitmap, K.matrix, null);		
		K.stage.unlockCanvas();
	}

	public void mainUpdate() {
		int mainIndex = _mainList.size();
		while (mainIndex-- > 0) {
			UpdateHelper(_mainList.get(mainIndex));
		}
	}
	
	public void mainRender() {
		int mainIndex = 0;
		int mainCount = _mainList.size();
		while (mainIndex < mainCount) {
			RenderHelper(_mainList.get(mainIndex++));
		}
	}
	
	public void UpdateHelper(KEntity entity) {
		entity.update();
		KHitDetector.ScanningLayer(entity);
		entity.x += entity.vx;
		entity.y += entity.vy;
		entity.vx = entity.vy = 0;
	}
	
	public void RenderHelper(KEntity entity) {
		entity.render();
		if (entity.visible && entity.renderData != null) {
			//K.render.drawBitmap(entity.renderData, entity.x + entity.mx,entity.y + entity.my, null);
			entity.getMatrix().setTranslate(entity.x + entity.mx, entity.y + entity.my);
			//entity.getMatrix().postScale(K.SysRW, K.SysRH);
			K.render.drawBitmap(entity.renderData, entity.getMatrix(), null);
		}
	}
}